/**
 * @author yye0682
 *
 * 
 */
package it.pecuswap.backend.ejb;

import it.pecuswap.backend.exceptions.ConfigurationException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * The Class Configuration.
 */
/**
 * @author yye0682
 * 
 */
public class Configuration {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = Logger.getLogger(Configuration.class);

    /** The file reference. */
    private String fileReference;

    /** The properties. */
    private Properties properties;

    /**
     * L'istanza di classe.
     */
    private static Configuration instance;

    /**
     * ...
     * 
     * @throws ConfigurationException
     *             the configuration exception
     */
    private Configuration() throws ConfigurationException {
    	acquireFileReference();
    	acquireProperties();
    }

    /**
     * Rileva l'istanza di classe, definendola se non definita.
     * 
     * @return L'istanza di classe.
     * @throws ConfigurationException
     *             the configuration exception
     */
    public static synchronized Configuration getInstance() throws ConfigurationException {
    	if (instance == null) {
    		instance = new Configuration();
    	}

    	return instance;
    }

    /**
     * Acquire file reference.
     * 
     * @throws ConfigurationException
     *             the configuration exception
     */
    private void acquireFileReference() throws ConfigurationException {
	try {
	    Properties configurationFileReferenceProperties = new Properties();
	    InputStream resourceStream = this.getClass().getResourceAsStream(
		    "/configuration-file-reference.properties");
	    configurationFileReferenceProperties.load(resourceStream);
	    fileReference = configurationFileReferenceProperties
		    .getProperty("configuration-file-reference");
	} catch (IOException e) {
	    throw new ConfigurationException(e);
	}
    }

    /**
     * Acquire properties.
     * 
     * @throws ConfigurationException
     *             the configuration exception
     */
    private void acquireProperties() throws ConfigurationException {
	this.properties = new Properties();

	try {
	    File propertiesFile = new File(fileReference);
	    FileInputStream fileInputStream = new FileInputStream(
		    propertiesFile);

	    this.properties.load(fileInputStream);
	} catch (FileNotFoundException e) {
	    throw new ConfigurationException(e);
	} catch (IOException e) {
	    throw new ConfigurationException(e);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    public Object clone() throws CloneNotSupportedException {
	throw new CloneNotSupportedException();
    }

    /**
     * ...
     * 
     * @param propertyKey
     *            the property key
     * @return the value of
     */
    public String getValueOf(String propertyKey) {
    	return this.properties.getProperty(propertyKey);
    }

}